# 1连接数据库
import pymysql
host = 'localhost'
port = 3308
user = 'root'
password = '123456'
database = 'market'
charset = 'utf8'
def get_conn():
    try:
        db=pymysql.Connect(host=host,port=port,user=user,password=password,database=database,charset=charset)
    except Exception:
        print('数据库连接有误')
    # else:
        # print('Connection Successfully')
    return db

# 2读取数据库
def read_db(sql):
    data=None
    db=get_conn()
    cursor=db.cursor()
    try:
        cursor.execute(sql)
        data=cursor.fetchall()
    except Exception as e:
        print(e)
    return data

# 3写入数据库
def wirte_db(sql):
    row=0
    db=get_conn()
    cursor=db.cursor()
    try:
        rows=cursor.execute(sql)
        db.commit()
    except Exception as e:
        db.rollback()
        print(e)
    return rows







if __name__ == '__main__':
    # get_conn()
    # sql=f"select * from products"
    sql= f"select sum(count) from orders"
    print(read_db(sql))